package com.powernode.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.BusinessEnum;
import com.powernode.domain.MemberCollection;
import com.powernode.domain.Prod;
import com.powernode.ex.handler.BusinessException;
import com.powernode.feign.MemberProdFeign;
import com.powernode.mapper.MemberCollectionMapper;
import com.powernode.model.Result;
import com.powernode.service.MemberCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MemberCollectionServiceImpl extends ServiceImpl<MemberCollectionMapper, MemberCollection> implements MemberCollectionService{

    @Autowired
    private MemberCollectionMapper memberCollectionMapper;

    @Autowired
    private MemberProdFeign memberProdFeign;

    @Override
    public Integer queryMemberCollectionProdCount(String openId) {
        Long count = memberCollectionMapper.selectCount(new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId, openId)
        );
        return Integer.valueOf(count.toString());
    }

    @Override
    public Page<Prod> queryMemberCollectionProdPage(Page<Prod> page, String openId) {
        // 创建会员收藏商品关系分页对象
        Page<MemberCollection> memberCollectionPage = new Page<>(page.getCurrent(),page.getSize());
        // 分页查询会员收藏商品的关系
        memberCollectionPage = memberCollectionMapper.selectPage(memberCollectionPage,new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId,openId)
                .orderByDesc(MemberCollection::getCreateTime)
        );
        // 从会员收藏商品分页对象中获取会员收藏商品关系集合
        List<MemberCollection> memberCollectionList = memberCollectionPage.getRecords();
        // 判断会员收藏商品关系集合是否有值
        if (CollectionUtils.isEmpty(memberCollectionList) || memberCollectionList.size() == 0) {
            return page;
        }
        // 从会员收藏商品关系集合中获取收藏商品id集合
        List<Long> prodIdList = memberCollectionList.stream()
                .map(MemberCollection::getProdId)
                .collect(Collectors.toList());
        // 远程接口调用：根据商品id集合查询商品对象集合
        Result<List<Prod>> result = memberProdFeign.getProdListByIds(prodIdList);
        if (BusinessEnum.OPERATION_FAIL.getCode().equals(result.getCode())) {
            throw new BusinessException("根据商品id集合查询商品对象集合失败");
        }
        // 获取商品对象集合
        List<Prod> prodList = result.getData();
        page.setRecords(prodList);
        page.setTotal(memberCollectionPage.getTotal());
        page.setPages(memberCollectionPage.getPages());
        return page;
    }

    @Override
    public Integer addOrCancelProd(String openId, Long prodId) {
        // 根据openid和商品标识查询当前收藏的记录
        MemberCollection memberCollection = memberCollectionMapper.selectOne(new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId, openId)
                .eq(MemberCollection::getProdId, prodId)
        );
        // 判断是否收藏
        if (ObjectUtil.isNotNull(memberCollection)) {
            // 说明：当前会员已收藏该商品 -> 即操作：取消收藏当前商品
            return memberCollectionMapper.deleteById(memberCollection);
        }
        // 说明：当前会员没有收藏该商品 -> 即操作：添加收藏当前商品
        memberCollection = new MemberCollection();
        memberCollection.setProdId(prodId);
        memberCollection.setOpenId(openId);
        memberCollection.setCreateTime(new Date());
        return memberCollectionMapper.insert(memberCollection);
    }
}
